/*Copyright 2014 Douglas Soares

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package br.ds.twitter4a.rest;

import java.util.LinkedHashMap;
import java.util.Map;

import br.ds.twitter4a.rest.util.HTTPMethods;
import br.ds.twitter4a.rest.util.OAuthAttrs;
import br.ds.twitter4a.rest.util.OAuthUtils;

import com.loopj.android.http.AsyncHttpResponseHandler;

/**
 * Request for twitter login.
 */
public class TwitterRequestTokenRequest extends AbstractTwitterRequest {

    /**
     * The OAUTH_SIGNATURE key.
     */
    private static final String OAUTH_SIGNATURE = "oauth_signature";

    /**
     * The oauthParams
     */
    private final Map<String, String> oauthParams = new LinkedHashMap<String, String>();

    /** {@inheritDoc} */
    @Override
    public void execute(final AsyncHttpResponseHandler responseHandler) {
	addParameter(OAuthAttrs.OAUTH_TIMESTEMP.key(),
		OAuthAttrs.OAUTH_TIMESTEMP.value());

	addParameter(OAuthAttrs.OAUTH_NONCE.key(),
		OAuthAttrs.OAUTH_NONCE.value());

	addParameter(OAuthAttrs.OAUTH_CALLBACK.key(),
		OAuthAttrs.OAUTH_CALLBACK.value());

	addParameter(OAuthAttrs.OAUTH_CONSUMER_KEY.key(),
		OAuthAttrs.OAUTH_CONSUMER_KEY.value());

	addParameter(OAuthAttrs.OAUTH_VERSION.key(),
		OAuthAttrs.OAUTH_VERSION.value());

	addParameter(OAuthAttrs.SIGNATURE_METHOD.key(),
		OAuthAttrs.SIGNATURE_METHOD.value());

	addParameter(OAUTH_SIGNATURE, OAuthUtils.getSignature(HTTPMethods.POST,
		getUrl(), oauthParams));

	post(responseHandler);
    }

    /** {@inheritDoc} */
    @Override
    public String getUrl() {
	return BASE_URL + "/oauth/request_token";
    }

    /** {@inheritDoc} */
    @Override
    public void addParameter(final String key, final String value) {
	super.addParameter(key, value);
	oauthParams.put(key, value);
    }
}
